<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head th:replace="include/common-header :: common_header(~{::title},~{::link})">
    <title>工作人员预约规则</title>
</head>
<body class="gray-bg">

<!--页面内容开始-->
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox ">
                <div class="ibox-title">
                    <h5>工作人员设置 </h5>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="table-responsive col-md-6">
                            <div class="m-l-xs toolbarAtRule">
                                <!--<h4>
                                    <button class="btn btn-sm btn-primary" type="button"
                                            onclick="atruleListObj.addWorkUserHandle()"><i
                                            class="fa fa-plus"></i>&nbsp;新增工作人员
                                    </button>
                                </h4>-->
                            </div>
                            <table id="atruleList_table" class="table table-striped"></table>
                        </div>
                        <div class="col-md-6">
                            <div class="panel panel-default">
                                <div class="panel-heading" id="atruleList_rule_heading">
                                    <span id="atruleList_rule_heading_title">例外日期</span>
                                    <a href="#" id="atruleList_btn_special" class="card-link pull-right"
                                       onclick="atruleListObj.specialHandle()">添加例外日期</a>
                                </div>
                                <div class="panel-body">
                                    <div class="row">
                                        <div class="col-md-12">
                                            <table id="atruleList_table_special" class="table table-striped"></table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!--页面内容结束-->
<div th:replace="include/common-js :: common_js"></div>

<!--弹窗：规则设置-->
<div th:replace="web/appoin/atrule-rule::atruleRule"></div>
<!--弹窗：例外设置-->
<div th:replace="web/appoin/atrule-special::atruleSpecial"></div>

<script type="text/javascript">
    $().ready(function () {
        atruleListObj.initTable();
        $('#atruleList_table_special').bootstrapTable({
            pagination: true,
            sidePagination: 'server',
            method: 'post',
            formatNoMatches: function () {  //没有匹配的结果
                return '暂无数据';
            },
            queryParams: function (param) {
                return param;
            },
            columns: [{
                title: '#',
                align: 'center',
                width: 50,
                formatter: function (value, row, index) {
                    return index + 1;
                }
            }, {
                title: '标题',
                field: 'title'
            }, {
                title: '日期',
                field: 'specialDate'
            }, {
                title: '是否工作',
                field: 'isWork',
                formatter: function (value, row, index) {
                    return value ? "工作" : "不工作";
                }
            }, {
                title: '操作',
                field: 'action',
                events: {
                    'click .edit': function (e, value, row, index) {
                        atruleListObj.specialHandle(row.id)
                    },
                    'click .trash': function (e, value, row, index) {
                        $.mySwalConfirm('将删除例外规则，确认吗？', function () {
                            $.myPost({
                                url: `/appoin/atspecial/delete`,
                                dataType: 'json',
                                data: [row.id],
                                success: function (data) {
                                    if (data && data.code === 0) {
                                        toastr.success(data.msg, '操作提示')
                                        atruleListObj.refreshSpecialDataList();
                                    } else {
                                        toastr.error(data.msg, '操作失败')
                                    }
                                }
                            })
                        });
                    }
                },
                formatter: function (value, row, index) {
                    var opers = [];
                    opers.push(`<a class="edit btn btn-white btn-bitbucket text-success" data-toggle="tooltip" data-placement="top" title="编辑">
                                    <i class="fa fa-edit"></i>
                                </a>`)
                    opers.push(`<a class="trash btn btn-white btn-bitbucket text-success" data-toggle="tooltip" data-placement="top" title="删除">
                                    <i class="fa fa-trash"></i>
                                </a>`)
                    return opers.join('');
                }
            }],
            responseHandler: function (res) {
                return {
                    total: res.page.totalCount,
                    rows: res.page.list
                };
            }
        });
    })
    let atruleListObj = new Object({
        dataListSelections: [],
        myTable: undefined,
        ruleId: undefined,
        dataRule: {
            interval: {required: true},
            isAuto: {required: true}
        },
        addWorkUserHandle: function () {

        },
        ruleHandle: function (userCode) {
            atruleRuleObj.init(userCode);
        },
        specialHandle: function (id) {
            if (!atruleListObj.ruleId) {
                $.mySwalWarn("请选择人员")
                return;
            }
            atruleSpecialObj.init(atruleListObj.ruleId, id);
        },
        refreshDataList: function () {
            atruleListObj.myTable.bootstrapTable('refresh');
        },
        refreshSpecialDataList: function () {
            $('#atruleList_table_special').bootstrapTable('refresh');
        },
        initTable: function () {
            atruleListObj.myTable = $('#atruleList_table').bootstrapTable({
                toolbar: '.toolbarAtRule',
                /*pagination: true,
                sidePagination: 'server',*/
                url: '/appoin/atrule/workUserList',
                method: 'post',
                formatNoMatches: function () {  //没有匹配的结果
                    return '暂无数据';
                },
                queryParams: function (param) {
                    return param;
                },
                onClickRow: function (row, $element, field) {
                    if(row.ruleId){
                        atruleListObj.ruleId = row.ruleId;
                        $('#atruleList_rule_heading_title').text(`${row.name} 例外日期`);
                        $('#atruleList_table_special').bootstrapTable('refresh', {
                            url: `/appoin/atspecial/list/${row.ruleId}`
                        });
                        $('#atruleList_btn_special').attr('disabled', false);
                    }
                },
                columns: [{
                    title: '#',
                    align: 'center',
                    width: 50,
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                }, {
                    title: '工作人员',
                    field: 'name'
                }, {
                    title: '工作间隔（分钟）',
                    field: 'interval'
                }, {
                    title: '自动生成',
                    field: 'isAuto',
                    formatter: function (value, row, index) {
                        return value ? "是" : "否";
                    }
                },{
                    title: '操作',
                    field: 'action',
                    events: {
                        'click .edit': function (e, value, row, index) {
                            atruleListObj.ruleHandle(row.userCode)
                        }
                    },
                    formatter: function (value, row, index) {
                        let opers = [];
                        opers.push(`<a class="edit btn btn-white btn-bitbucket text-success" data-toggle="tooltip" data-placement="top" title="编辑规则">
                                    <i class="fa fa-edit"></i>
                                </a>`)
                        return opers.join('');
                    }
                }],
                responseHandler: function (res) {
                    return res.list;
                }
            });
        },
        dataFormSubmit: function (form) {
            let formVals = $(form).serializeArray();
            let data = {}
            for (let i = 0; i < formVals.length; i++) {
                data[formVals[i].name] = formVals[i].value;
            }
            $.myPost({
                url: `/appoin/atrule/${data.id ? 'update' : 'save'}`,
                dataType: 'json',
                data: data,
                success: function (data) {
                    if (data && data.code === 0) {
                        toastr.success(data.msg, '操作提示')
                        atruleListObj.myTable.bootstrapTable('refresh');
                    } else {
                        toastr.error(data.msg, '操作失败')
                    }
                }
            })
        }
    })
</script>
</body>
</html>